package display_items;
use strict;
use warnings;

use lib::RokuUI;

my $display;

# ---------------------------------------------------------------------
# Constructor
sub new {
    my ($class, %params) = @_;
	my $self = {};
	$self->{'items'} = $params{'items'};
	bless $self, $class;
};
# ---------------------------------------------------------------------

sub action {
	my @items; # array to hold all items to display
	my $item;  # one single item
	my $rc;    # return value from remote control

	my $self=shift;
	$self->{'display'}->clear();

	while (1) {
		@items=$self->get_items();
		foreach my $item (@items) {
			# marquee is very limited (only top line and not possible to display the second line as well)
			$rc = $self->{'display'}->msg(text =>  $item, duration => 15, keygrab => 1, mode => 'marquee');

			# Check for key pressed, act corresponding (and return the key)
			if ($rc eq 'CK_PURPLE' || $rc eq 'CK_EXIT' || $rc eq 'CK_POWER') {
				$self->{'display'}->cmd("sketch -c clear");
				$self->{'display'}->cmd("sketch -c exit");
				return $rc;
			}
		}
    };
}

# ---------------------------------------------------------------------
# get_items is a function to retrieve the actual items to display.
# this will be overwritten in a derived class.
sub get_items {
	my $self=shift;
	my @items=@{$self->{'items'}};

	if ($#items < 0) {
		@items = ('No items');
	}
	return @items
}

1;
